package com.right.health.app.ui.me;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.ServiceItemBean;
import com.right.health.app.bean.ServicePackResponse;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.net.HttpConstants;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.ServiceItemAdapter;
import com.right.health.app.utils.GlideUtil;
import com.right.health.app.widget.ProgressWebView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @author: weming
 * @date: 2021/4/24
 * @description:单项服务包详情
 */
public class ServiceDetailActivity extends BaseActivity implements OnHttpListener, View.OnClickListener {
    private static final int HTTP_CODE_GET_DATA= 1;
    public static final int HTTP_CODE_IS_ORDER=2;
    public static final int HTTP_CODE_ORDER=4;
    private final int HTTP_CODE_COMPLETE = 3;

    private ListView mListView;
    private ProgressWebView mWebView;
    private ImageView mImageView;
    private ServiceItemAdapter mAdapter;
    private UserApi mUserApi;
    private ProgressDialog mProgressDialog;
    private TextView mTitleTv,mHospitalTv,mDescTv,mTopTv;
    private Button mOrderBtn;
    private ServicePackResponse.ServicesBean mBean;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_service_detail);
    }

    @Override
    public void findView() {
        mListView = findViewById(R.id.listView);
        mTitleTv=findViewById(R.id.tv_title);
        mOrderBtn = findViewById(R.id.btn_order);
        mDescTv = findViewById(R.id.tv_desc);
        mHospitalTv=findViewById(R.id.tv_hospital);
        mTopTv=findViewById(R.id.tv_top_title);
        mWebView = findViewById(R.id.web_view);
//        mImageView = findViewById(R.id.iv_detail);
        mAdapter = new ServiceItemAdapter(this);
        mListView.setAdapter(mAdapter);
        mOrderBtn.setOnClickListener(this);
        mAdapter.setClickListener(this);
    }

    @Override
    public void initialize() {
        mBean=(ServicePackResponse.ServicesBean)getIntent().getSerializableExtra("data");
        mTitleTv.setText(mBean.getPkname());
        mTopTv.setText(mBean.getPkname());
        mHospitalTv.setText("服务医院："+mBean.getHospital());
        mDescTv.setText(mBean.getPkdesc());
        mUserApi = new UserApi();
        mUserApi.setListener(this);
        mProgressDialog = ProgressDialog.show(this, "", "加载中",false,true);
        mUserApi.addRequestCode(HTTP_CODE_IS_ORDER);
        mUserApi.isServicePackOrder(mBean.getPkid());
        String url = AppConfig.HTTP_ADDRESS+"/pandWx/" +mBean.getPkdetailpic().replace("\\","/" );
//        Log.e("ServiceDetailActivity", "initialize: "+ url);
//        Glide.with(this).load(url).into(mImageView);
        mWebView.loadUrl(url);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_order) {
            mProgressDialog = ProgressDialog.show(this, "", "加载中",false,true);
            mUserApi.addRequestCode(HTTP_CODE_ORDER);
            mUserApi.orderServicePack(mBean.getPkid());
        }else if (v.getId() == R.id.tv_complete) {
            int index = (int) v.getTag();
            ServiceItemBean bean = mAdapter.getItem(index);
            if (bean.getIsfinish().equals("0")) {
                mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
                mUserApi.setTag(index);
                mUserApi.addRequestCode(HTTP_CODE_COMPLETE);
                mUserApi.finishServicePack(bean.getDetailid());
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode==HTTP_CODE_GET_DATA) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    List<ServiceItemBean> list = new Gson().fromJson(jsonObject.optString("details"), new TypeToken<List<ServiceItemBean>>() {
                    }.getType());
                    mAdapter.setDataSource(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            } else if (HTTP_CODE_IS_ORDER == request.requestCode) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    List<ServiceItemBean> list = new Gson().fromJson(jsonObject.optString("details"), new TypeToken<List<ServiceItemBean>>() {
                    }.getType());
                    if (CollectionUtil.isEmpty(list)) {
                        mOrderBtn.setVisibility(View.VISIBLE);
//                        mUserApi.addRequestCode(HTTP_CODE_GET_DATA);
//                        mUserApi.getServiceDetail(mBean.getPkid());
                    }else {
                        mOrderBtn.setVisibility(View.GONE);
                        mAdapter.setOrder(true);
                        mAdapter.setDataSource(list);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            }else if (HTTP_CODE_ORDER == request.requestCode) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    JSONObject orderObj = jsonObject.optJSONObject("order");
                    String orderCode = orderObj.optString("ordercode");
                    String orderId=orderObj.optString("orderid");
                    ServiceOrderActivity.launch(this,orderId,orderCode,mBean.getPkname(),mBean.getHospital(),mBean.getPkprice(),1000);
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            } else if (request.requestCode == HTTP_CODE_COMPLETE) {
                ToastUtil.show(this, "操作成功");
                try {
                    mAdapter.getItem((int)request.tag).setIsfinish("1");
                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            mUserApi.addRequestCode(HTTP_CODE_IS_ORDER);
            mUserApi.isServicePackOrder(mBean.getPkid());
        }
    }
}
